package com.youku.expression.ast;

import java.util.List;

import com.youku.expression.environment.Environment;

/**
 * 语句块
 * 
 * @author loda
 *
 */
public class BlockAst extends Ast {

	public BlockAst(Ast... asts) {
		super(asts);
	}

	public BlockAst(List<Ast> asts) {
		super(asts);
	}

	public List<Ast> statements() {
		return children();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for (Ast statement : statements()) {
			builder.append(statement.toString()).append(";");
		}
		return "{" + builder.toString() + "}";
	}

	@Override
	public Object eval(Environment environment) {
		Object result = null;
		for (Ast stmt : statements()) {
			if (stmt instanceof NullStatementAst) {
				break;
			} else {
				result = stmt.eval(environment);
			}

		}

		return result;
	}

}
